﻿@page "/stockout-detail/{Id:int}"
@using Microsoft.EntityFrameworkCore
@using Wms.Core.Entities
@inject Wms.Data.WarehouseDbContext Db
@inject NavigationManager Navigation

<h3 class="mb-4 text-danger">出库单明细</h3>

@if (order == null)
{
    <div class="text-center py-5">
        <div class="spinner-border text-primary" role="status"></div>
        <p class="mt-3 text-muted">加载中...</p>
    </div>
}
else
{
    <div class="card shadow-sm mb-4">
        <div class="card-body">
            <h5 class="card-title">单据信息</h5>
            <p><strong>出库单号：</strong>@order.OrderNo</p>
            <p><strong>日期：</strong>@order.Date.ToString("yyyy-MM-dd HH:mm")</p>
        </div>
    </div>

    <div class="card shadow-sm">
        <div class="card-body">
            <h5 class="card-title">出库明细</h5>
            @if (order.Details == null || !order.Details.Any())
            {
                <p class="text-muted">暂无明细记录</p>
            }
            else
            {
                <table class="table table-striped">
                    <thead>
                        <tr>
                            <th>物料</th>
                            <th>数量</th>
                            <th>库位</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (var d in order.Details)
                        {
                            <tr>
                                <td>@d.Item?.Name</td>
                                <td>@d.Quantity</td>
                                <td>@d.Location?.Code</td>
                            </tr>
                        }
                    </tbody>
                </table>
            }
        </div>
    </div>

    <div class="mt-4">
        <button class="btn btn-secondary" @onclick="Back">返回</button>
    </div>
}

@code {
    [Parameter] public int Id { get; set; }
    private StockOutOrder? order;

    protected override async Task OnInitializedAsync()
    {
        order = await Db.StockOutOrders
            .Include(o => o.Details)
                .ThenInclude(d => d.Item)
            .Include(o => o.Details)
                .ThenInclude(d => d.Location)
            .FirstOrDefaultAsync(o => o.Id == Id);
    }

    private void Back()
    {
        Navigation.NavigateTo("/stockout-list");
    }
}
